In [1]:
import plotly.offline as pyo

from plotly.graph_objs import *

import chart_studio.plotly as py

import pandas as pd
from pandas import DataFrame
In [2]:
pyo.offline.init_notebook_mode()
In [3]:
pyo.iplot([{'x' : [1,2,3],
        'y' : [5,6,7],
        'marker' : {'size' : [100, 200, 300],
                   'sizemode' : 'area'}}])
In [4]:
pyo.iplot([{'x' : [1,2,3],
        'y' : [5,6,7],
        'marker' : {'size' : [10000, 20000, 30000],
                   'sizemode' : 'area'}}])
In [5]:
pyo.iplot([{'x' : [1,2,3],
        'y' : [5,6,7],
        'marker' : {'size' : [10000, 20000, 30000],
                   'sizemode' : 'area',
                   'sizeref' : 10}}])
In [6]:
regionalData = pd.read_html("https://en.wikipedia.org/wiki/Regions_of_England", header=0)
In [11]:
regionalData[4]
Out[11]:
Name[28] Population[29] Counties % pop. change fromprevious year[29] Area[29] Population density[29] Median gross annualearnings (£) 2019[30] % of population claiming IncomeSupport or JSA (August 2012) % as atAugust 2001 Largest urban area
0 South East 9217265 Berkshire Buckinghamshire East Sussex Hampshir... +0.51% 19,072 km2 (7,364 sq mi) 481/km2 (1,250/sq mi) 32120 3.0% 5.4% South Hampshire [note 1]
1 London 9002488 City of London Greater London +0.61% 1,572 km2 (607 sq mi) 5,701/km2 (14,770/sq mi) 38992 5.3% 10.1% Greater London Built-up Area
2 North West 7341196 Cheshire Cumbria Greater Manchester Lancashire... +0.67% 14,108 km2 (5,447 sq mi) 520/km2 (1,300/sq mi) 28137 5.3% 10.4% Greater Manchester Built-up Area
3 East of England 6236072 Bedfordshire Cambridgeshire Essex Hertfordshir... +0.56% 19,116 km2 (7,381 sq mi) 326/km2 (840/sq mi) 30345 3.5% 6.2% Southend Urban Area [note 2]
4 West Midlands 5934037 Herefordshire Shropshire Staffordshire Warwick... +0.56% 12,998 km2 (5,019 sq mi) 457/km2 (1,180/sq mi) 28536 5.1% 9.2% West Midlands Conurbation
5 South West 5624696 Bristol Cornwall Devon Dorset Gloucestershire ... +0.45% 23,836 km2 (9,203 sq mi) 236/km2 (610/sq mi) 28654 3.3% 6.8% Bristol Built-up Area
6 Yorkshire and the Humber 5502967 East Riding of Yorkshire North Yorkshire (exce... +0.43% 15,405 km2 (5,948 sq mi) 357/km2 (920/sq mi) 27835 5.2% 9.3% West Yorkshire Built-up Area
7 East Midlands 4835928 Derbyshire Leicestershire Lincolnshire (except... +0.66% 15,625 km2 (6,033 sq mi) 309/km2 (800/sq mi) 28000 4.2% 7.7% Nottingham Urban Area
8 North East 2669941 County Durham Northumberland Tyne and Wear Nor... +0.45% 8,579 km2 (3,312 sq mi) 311/km2 (810/sq mi) 27187 6.1% 11.6% Tyneside
9 England 56286961 48 +0.55% 130,311 km2 (50,313 sq mi) 432/km2 (1,120/sq mi) 30667 4.45% 8.32%[31] London
In [12]:
regions = regionalData[4][regionalData[4]['Name[28]'] != 'England']
regions
Out[12]:
Name[28] Population[29] Counties % pop. change fromprevious year[29] Area[29] Population density[29] Median gross annualearnings (£) 2019[30] % of population claiming IncomeSupport or JSA (August 2012) % as atAugust 2001 Largest urban area
0 South East 9217265 Berkshire Buckinghamshire East Sussex Hampshir... +0.51% 19,072 km2 (7,364 sq mi) 481/km2 (1,250/sq mi) 32120 3.0% 5.4% South Hampshire [note 1]
1 London 9002488 City of London Greater London +0.61% 1,572 km2 (607 sq mi) 5,701/km2 (14,770/sq mi) 38992 5.3% 10.1% Greater London Built-up Area
2 North West 7341196 Cheshire Cumbria Greater Manchester Lancashire... +0.67% 14,108 km2 (5,447 sq mi) 520/km2 (1,300/sq mi) 28137 5.3% 10.4% Greater Manchester Built-up Area
3 East of England 6236072 Bedfordshire Cambridgeshire Essex Hertfordshir... +0.56% 19,116 km2 (7,381 sq mi) 326/km2 (840/sq mi) 30345 3.5% 6.2% Southend Urban Area [note 2]
4 West Midlands 5934037 Herefordshire Shropshire Staffordshire Warwick... +0.56% 12,998 km2 (5,019 sq mi) 457/km2 (1,180/sq mi) 28536 5.1% 9.2% West Midlands Conurbation
5 South West 5624696 Bristol Cornwall Devon Dorset Gloucestershire ... +0.45% 23,836 km2 (9,203 sq mi) 236/km2 (610/sq mi) 28654 3.3% 6.8% Bristol Built-up Area
6 Yorkshire and the Humber 5502967 East Riding of Yorkshire North Yorkshire (exce... +0.43% 15,405 km2 (5,948 sq mi) 357/km2 (920/sq mi) 27835 5.2% 9.3% West Yorkshire Built-up Area
7 East Midlands 4835928 Derbyshire Leicestershire Lincolnshire (except... +0.66% 15,625 km2 (6,033 sq mi) 309/km2 (800/sq mi) 28000 4.2% 7.7% Nottingham Urban Area
8 North East 2669941 County Durham Northumberland Tyne and Wear Nor... +0.45% 8,579 km2 (3,312 sq mi) 311/km2 (810/sq mi) 27187 6.1% 11.6% Tyneside
In [16]:
regions['Area[29]'] = regions['Area[29]'].apply(lambda x: int(x.split("\xa0")[0].replace(',','')))
regions
/var/folders/yn/ks28gqsn3gx0pqmlyn_fzq780000gq/T/ipykernel_57447/401246338.py:1: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Out[16]:
Name[28] Population[29] Counties % pop. change fromprevious year[29] Area[29] Population density[29] Median gross annualearnings (£) 2019[30] % of population claiming IncomeSupport or JSA (August 2012) % as atAugust 2001 Largest urban area
0 South East 9217265 Berkshire Buckinghamshire East Sussex Hampshir... +0.51% 19072 481/km2 (1,250/sq mi) 32120 3.0% 5.4% South Hampshire [note 1]
1 London 9002488 City of London Greater London +0.61% 1572 5,701/km2 (14,770/sq mi) 38992 5.3% 10.1% Greater London Built-up Area
2 North West 7341196 Cheshire Cumbria Greater Manchester Lancashire... +0.67% 14108 520/km2 (1,300/sq mi) 28137 5.3% 10.4% Greater Manchester Built-up Area
3 East of England 6236072 Bedfordshire Cambridgeshire Essex Hertfordshir... +0.56% 19116 326/km2 (840/sq mi) 30345 3.5% 6.2% Southend Urban Area [note 2]
4 West Midlands 5934037 Herefordshire Shropshire Staffordshire Warwick... +0.56% 12998 457/km2 (1,180/sq mi) 28536 5.1% 9.2% West Midlands Conurbation
5 South West 5624696 Bristol Cornwall Devon Dorset Gloucestershire ... +0.45% 23836 236/km2 (610/sq mi) 28654 3.3% 6.8% Bristol Built-up Area
6 Yorkshire and the Humber 5502967 East Riding of Yorkshire North Yorkshire (exce... +0.43% 15405 357/km2 (920/sq mi) 27835 5.2% 9.3% West Yorkshire Built-up Area
7 East Midlands 4835928 Derbyshire Leicestershire Lincolnshire (except... +0.66% 15625 309/km2 (800/sq mi) 28000 4.2% 7.7% Nottingham Urban Area
8 North East 2669941 County Durham Northumberland Tyne and Wear Nor... +0.45% 8579 311/km2 (810/sq mi) 27187 6.1% 11.6% Tyneside
In [22]:
regions['text'] = regions.apply(lambda x: "<b>{}</b><br>Area: {} Km<sup>2</sup>".format(x['Name[28]'], x['Area[29]']), axis = 1)
regions
/var/folders/yn/ks28gqsn3gx0pqmlyn_fzq780000gq/T/ipykernel_57447/1602785705.py:1: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Out[22]:
Name[28] Population[29] Counties % pop. change fromprevious year[29] Area[29] Population density[29] Median gross annualearnings (£) 2019[30] % of population claiming IncomeSupport or JSA (August 2012) % as atAugust 2001 Largest urban area text
0 South East 9217265 Berkshire Buckinghamshire East Sussex Hampshir... +0.51% 19072 481/km2 (1,250/sq mi) 32120 3.0% 5.4% South Hampshire [note 1] <b>South East</b><br>Area: 19072 Km<sup>2</sup>
1 London 9002488 City of London Greater London +0.61% 1572 5,701/km2 (14,770/sq mi) 38992 5.3% 10.1% Greater London Built-up Area <b>London</b><br>Area: 1572 Km<sup>2</sup>
2 North West 7341196 Cheshire Cumbria Greater Manchester Lancashire... +0.67% 14108 520/km2 (1,300/sq mi) 28137 5.3% 10.4% Greater Manchester Built-up Area <b>North West</b><br>Area: 14108 Km<sup>2</sup>
3 East of England 6236072 Bedfordshire Cambridgeshire Essex Hertfordshir... +0.56% 19116 326/km2 (840/sq mi) 30345 3.5% 6.2% Southend Urban Area [note 2] <b>East of England</b><br>Area: 19116 Km<sup>2...
4 West Midlands 5934037 Herefordshire Shropshire Staffordshire Warwick... +0.56% 12998 457/km2 (1,180/sq mi) 28536 5.1% 9.2% West Midlands Conurbation <b>West Midlands</b><br>Area: 12998 Km<sup>2</...
5 South West 5624696 Bristol Cornwall Devon Dorset Gloucestershire ... +0.45% 23836 236/km2 (610/sq mi) 28654 3.3% 6.8% Bristol Built-up Area <b>South West</b><br>Area: 23836 Km<sup>2</sup>
6 Yorkshire and the Humber 5502967 East Riding of Yorkshire North Yorkshire (exce... +0.43% 15405 357/km2 (920/sq mi) 27835 5.2% 9.3% West Yorkshire Built-up Area <b>Yorkshire and the Humber</b><br>Area: 15405...
7 East Midlands 4835928 Derbyshire Leicestershire Lincolnshire (except... +0.66% 15625 309/km2 (800/sq mi) 28000 4.2% 7.7% Nottingham Urban Area <b>East Midlands</b><br>Area: 15625 Km<sup>2</...
8 North East 2669941 County Durham Northumberland Tyne and Wear Nor... +0.45% 8579 311/km2 (810/sq mi) 27187 6.1% 11.6% Tyneside <b>North East</b><br>Area: 8579 Km<sup>2</sup>
In [24]:
regionTrace = {'type' : 'scatter',
              'mode' : 'markers',
              'x' : regions['Population[29]'],
              'y' : regions['Median gross annualearnings (£) 2019[30]'],
              'text' : regions['text'],
              'hoverinfo' : 'text+x+y'}
In [26]:
layout = {'title' : 'Population, Median Earnings and Area of English Regions',
         'xaxis' : {'title' : 'Population',
                   'range' : [regions['Population[29]'].min() * 0.95, 
                             regions['Population[29]'].max() * 1.05]},
         'yaxis' : {'title' : 'Median Earnings',
                   'tickformat' : ',',
                   'tickprefix' : '£',
                   'range' : [regions['Median gross annualearnings (£) 2019[30]'].min() * 0.95, 
                             regions['Median gross annualearnings (£) 2019[30]'].max() * 1.05]}}

fig = Figure(data=[regionTrace], layout=layout)
pyo.iplot(fig)
In [29]:
regionTrace.update({'marker' : {'size' : regions['Area[29]'],
                               'sizemode' : 'area'}})
fig = Figure(data=[regionTrace], layout=layout)
pyo.iplot(fig)
In [30]:
regionTrace['marker'].update({'sizeref' : 5 })
fig = Figure(data=[regionTrace], layout=layout)
pyo.iplot(fig)
In [31]:
fig['layout']['xaxis'].update({'range' : [regions['Population[29]'].min() * 0.75, 
                                         regions['Population[29]'].max() * 1.05]})

fig['layout']['yaxis'].update({'range' : [regions['Median gross annualearnings (£) 2019[30]'].min() * 0.75, 
                                         regions['Median gross annualearnings (£) 2019[30]'].max() * 1.05]})

pyo.iplot(fig)
In [ ]: